package com.ehotting.edsta.circuitDesign.bean;

import com.ehotting.edsta.circuitDesign.bean.request.RequestCodeExecute;
import com.ehotting.edsta.circuitDesign.domain.EsElement;
import com.ehotting.edsta.circuitDesign.enums.*;
import org.apache.commons.lang3.SerializationUtils;

import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;

/***
 * 电路包
 * key {@link SignalTypeEnum#getCode()} }
 */

public class CircuitPackageBean extends HashMap<String,CircuitGroupBean> implements Cloneable{

    public List<Map<String, Object>>  getPropList(){
        List<CircuitElementBean> circuitElementList = getCircuitGroupBean();
        List<Map<String, Object>> allList = circuitElementList.stream().map(t->{return t.getCircuit().getProp();}).collect(Collectors.toList());
        return allList;
    }

    public List<CircuitElementBean> getCircuitGroupBean(){
        Set<Entry<String,CircuitGroupBean>> circuitGroupList =  this.entrySet();

        List<CircuitElementBean> circuitElementList = new ArrayList<>();
        for(Entry<String,CircuitGroupBean> entry : circuitGroupList){
            CircuitGroupBean circuitGroup = entry.getValue();
            circuitElementList.addAll(circuitGroup.getCircuitElementList());
        }
        return circuitElementList;
    }

    public Integer getAnalogueOutChannelCount(){
        Integer count = 0;
        Collection<CircuitGroupBean> list = this.values();
        for(CircuitGroupBean cg : list){
            count += cg.getAllOutChannelCount4Analogue();
        }
        return count;
    }

    public Integer getDigitalOutChannelCount(){
        Integer count = 0;
        Collection<CircuitGroupBean> list = this.values();
        for(CircuitGroupBean cg : list){
            count += cg.getAllOutChannelCount4Digital();
        }
        return count;
    }

    /**
     *方案模拟通道，通道数总和
     * @return
     */
    public Integer getAnalogueChannelCount(){
        return getAnalogueOutChannelCount()+getDigitalOutChannelCount();
    }

    /**
     * 方案的模拟电路个数
     * @return
     */
    public Integer getAnalogueCircuitCount(){
        int pgAnalogueCount = 0;
        String code = ChannelTypeInputEnum.ANALOGUE.getCode();
        Set<String> analogueKeySet = this.keySet().stream().filter(k -> k.contains(code)).collect(Collectors.toSet());
        for(String a : analogueKeySet){
            Collection<CircuitElementBean> values = this.get(a).values();
            for(CircuitElementBean v : values){
                pgAnalogueCount += v.getCount();
            }
        }
        return pgAnalogueCount;
    }

    /**
     * 方案的数字输入模拟通道，通道数总和
     * @return
     */
    public Integer getDigitalChannelCount(){
        Integer bigChanCount = 0;
        Collection<CircuitGroupBean> list = this.values();
        for(CircuitGroupBean cg : list){
            for(Long key : cg.keySet()){
                CircuitElementBean value = cg.get(key);
                if(value.getChannelTypeInput().equals(ChannelTypeInputEnum.DIGITAL)){
                    bigChanCount += value.getChannelNum()*value.getCount();
                }
            }
        }
        return bigChanCount;
    }

    /**
     * 方案最大采样长度
     * @return
     */
    public Integer getMaxSamplingLength(){
        Integer maxSamplingLength = 0;
        Collection<CircuitGroupBean> list = this.values();
        for(CircuitGroupBean cg : list){
            for(Long key : cg.keySet()){
                CircuitElementBean value = cg.get(key);
                if(value.getChannelTypeInput().equals(ChannelTypeInputEnum.ANALOGUE)){
                    if(value.getSamplingLength() > maxSamplingLength){
                        maxSamplingLength = value.getSamplingLength();
                    }
                }
            }
        }
        return maxSamplingLength;
    }

    /**
     * 方案最大adc分辨率（模拟输出时，电路位数属性等于adc分辨率）
     * @return
     */
    public Integer getMaxAdcSamplingBit(){
        Integer maxAdcSamplingBit = 0;
        Collection<CircuitGroupBean> list = this.values();
        for(CircuitGroupBean cg : list){
            Set<Long> set = cg.keySet();
            for(Long key : set){
                CircuitElementBean value = cg.get(key);
                if(value.getChannelTypeOut() != null && value.getChannelTypeOut().equals(ChannelTypeOutEnum.ANALOGUE)){
                    if(value.getNumberOfDigits() > maxAdcSamplingBit){
                        maxAdcSamplingBit = value.getNumberOfDigits();
                    }
                }
            }
        }
        return maxAdcSamplingBit;
    }


    /**
     * 最大采样率
     * @return
     */
    public Integer getMaxSample(){
        Integer MaxSample = 0;
        Collection<CircuitGroupBean> list = this.values();
        for(CircuitGroupBean cg : list){
            for(Long key : cg.keySet()){
                CircuitElementBean value = cg.get(key);
                if(value.getSample()>MaxSample){
                    MaxSample = value.getSample();
                }
            }
        }
        return MaxSample;
    }


    @Override
    public CircuitPackageBean clone() {
        return (CircuitPackageBean) SerializationUtils.clone(this);
    }


}